package org.stvd.core.jdbc;

import java.util.List;

import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;

public class Pagination<T> {

    public static final int NUMBERS_PER_PAGE = 20;
    // 一页显示的记录数
    private int numPerPage;
    // 记录总数
    private int totalRows;
    // 总页数
    private int totalPages;
    // 当前页码
    private int currentPage;
    // 起始行数
    private int startIndex;
    // 结束行数
    private int lastIndex;
    // 结果集存放List
    private List<T> resultList;

    /**
     * 分页构造函数
     */
    public Pagination(String sql, Object[] parameters, int currentPage, int numPerPage, Class<T> cl, JdbcTemplate jdbcTemplate) {
        // 设置每页显示记录数
        setNumPerPage(numPerPage);
        // 设置要显示的页数
        setCurrentPage(currentPage);
        // 计算总记录数
        StringBuffer totalSQL = new StringBuffer(" SELECT count(0) FROM ( ");
        totalSQL.append(sql).append(" ) totalTable ");
        // 总记录数
        setTotalRows(jdbcTemplate.queryForObject(totalSQL.toString(), parameters, Integer.class));
        // 计算总页数
        setTotalPages();
        // 计算起始行数
        setStartIndex();
        // 计算结束行数
        setLastIndex();
        // 装入结果集
        List<T> resultList = null;
        try {
            if (parameters != null && parameters.length > 0)
                resultList = jdbcTemplate.query(getMySQLPageSQL(sql, startIndex, numPerPage), parameters, new BeanPropertyRowMapper<T>(cl));
            else
                resultList = jdbcTemplate.query(getMySQLPageSQL(sql, startIndex, numPerPage), new BeanPropertyRowMapper<T>(cl));
        } catch (Exception e) {
            e.printStackTrace();
        }
        setResultList(resultList);
    }

    /**
     * 构造MySQL数据分页SQL
     */
    public String getMySQLPageSQL(String queryString, Integer startIndex, Integer pageSize) {
        String result = "";
        if (null != startIndex && null != pageSize) {
            result = queryString + " limit " + startIndex + "," + pageSize;
        } else if (null != startIndex && null == pageSize) {
            result = queryString + " limit " + startIndex;
        } else {
            result = queryString;
        }
        return result;
    }

    public int getCurrentPage() {
        return currentPage;
    }

    public void setCurrentPage(int currentPage) {
        this.currentPage = currentPage;
    }

    public int getNumPerPage() {
        return numPerPage;
    }

    public void setNumPerPage(int numPerPage) {
        this.numPerPage = numPerPage;
    }

    public List<T> getResultList() {
        return resultList;
    }

    public void setResultList(List<T> resultList) {
        this.resultList = resultList;
    }

    public int getTotalPages() {
        return totalPages;
    }

    /**
     * 计算总页数
     */
    public final void setTotalPages() {
        if (totalRows % numPerPage == 0) {
            this.totalPages = totalRows / numPerPage;
        } else {
            this.totalPages = (totalRows / numPerPage) + 1;
        }
    }

    public int getTotalRows() {
        return totalRows;
    }

    public final void setTotalRows(int totalRows) {
        this.totalRows = totalRows;
    }

    public int getStartIndex() {
        return startIndex;
    }

    public final void setStartIndex() {
        this.startIndex = (currentPage - 1) * numPerPage;
    }

    public int getLastIndex() {
        return lastIndex;
    }

    /**
     * 计算结束时候的索引页
     */
    public final void setLastIndex() {
        System.out.println("totalRows=" + totalRows);
        System.out.println("numPerPage=" + numPerPage);
        if (totalRows < numPerPage) {
            this.lastIndex = totalRows;
        } else if ((totalRows % numPerPage == 0) || (totalRows % numPerPage != 0 && currentPage < totalPages)) {
            this.lastIndex = currentPage * numPerPage;
        } else if (totalRows % numPerPage != 0 && currentPage == totalPages) {
            this.lastIndex = totalRows;
        }
    }

}